<?php

class Admin_ProductController extends Zendvn_Controller_Action
{

    private $_arrParam;
    private $_mainAction;

    public function init()
    {
        $this->loadTemplate(TEMPLATE_PATH . '/admin');

        $this->view->headScript()->appendFile(TEMPLATE_URL . '/admin/js/form.js');
        $this->view->headScript()->appendFile(TEMPLATE_URL . '/admin/js/submit.js');
        $this->_arrParam = $this->_request->getParams();
        $this->_mainAction = $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';
        $this->view->arrParam = $this->_arrParam;
    }

    public function indexAction()
    {
        if (!isset($this->_arrParam['cat_id']))
            $this->_redirect(APPLICATION_URL . '/admin/category');
        $tblProduct = new Model_Table_Product();
        $this->view->Items = $tblProduct->allItems($this->_arrParam, 'cate');
    }

    public function addAction()
    {

        if ($this->_request->isPost())
        {
            $validator = new Admin_Form_ValidateProduct($this->_arrParam);
            if ($validator->isValid() == true)
            {
                $this->view->arrError = $validator->getErrors();
                $this->view->arrData = $validator->getData();
            }
            else
            {
                $data       = $validator->getData();
                $tblProduct = new Model_Table_Product();
                $tblProduct->addItem($data);
                $this->_redirect($this->_mainAction . '/cat_id/' . $data['cat_id']);
            }
        }
    }

    public function deleteAction()
    {
        $tblProduct = new Model_Table_Product();
        if ($this->_arrParam['dropdown'] == 'none')
        {
            $this->_redirect($this->_mainAction . '/cat_id/' . $this->_arrParam['cat_id']);
        }
        elseif ($this->_arrParam['type'] == 'multy' && $this->_arrParam['dropdown'] == 'delete')
        {
            $tblProduct->deleteItem($this->_arrParam, 'multy');
        }
        else
        {
            $tblProduct->deleteItem($this->_arrParam);
        }
        $this->_redirect($this->_mainAction . '/cat_id/' . $this->_arrParam['cat_id']);
        $this->_helper->viewRenderer->setNoRender();
    }

    public function editAction()
    {
        //Check Permission
        $info        = new Zendvn_System_Info();
        $tblProduct  = new Model_Table_Product();
        $username    = $info->getMemberInfo('username');
        $group_id    = $info->getMemberInfo('group_id');
        $ItemsDetail = $tblProduct->getItem($this->_arrParam);
        if ($group_id == 3 || $group_id == 4)
        {
            if ($ItemsDetail['created_by'] != $username)
            {
                $this->_redirect($_SERVER['HTTP_REFERER']);
            }
        }
        //Get Detail        
        $this->view->Item = $tblProduct->getItem($this->_arrParam);
        //Post
        if ($this->_request->isPost())
        {
            $validator = new Admin_Form_ValidateProduct($this->_arrParam);
            if ($validator->isValid() == true)
            {
                $this->view->arrError = $validator->getErrors();
                $this->view->arrData = $validator->getData();
            }
            else
            {
                echo '</br>' . 'Ko co loi';
                $data       = $validator->getData();
                $tblProduct = new Model_Table_Product();
                $tblProduct->editItem($data);
                $this->_redirect($this->_mainAction . '/cat_id/' . $data['cat_id']);
            }
        }
    }

    public function statusAction()
    {
        $tblProduct = new Model_Table_Product();
        $newStatus  = $tblProduct->changeStatus($this->_arrParam);

        $lnkImage  = APPLICATION_URL . '/public/templates/admin/images/';
        $lnkStatus = APPLICATION_URL . '/admin/product/status/id/' . $this->_arrParam['id'] .
                '/status/' . $newStatus;
        if ($newStatus == 1)
        {
            echo '<span class="pointer" onclick=loadPage("' . $this->_arrParam['id'] . '","' . $lnkStatus . '") ><img src="' . $lnkImage . 'active.png"></span>';
        }
        else
        {
            echo '<span class="pointer" onclick=loadPage("' . $this->_arrParam['id'] . '","' . $lnkStatus . '") ><img src="' . $lnkImage . 'inactive.png"></span>';
        }
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout()->disableLayout();
    }

    public function listAction()
    {
        $tblProduct = new Model_Table_Product();
        $page       = $this->_request->getParam('page', 1);
        $limit      = 25;
        $offset     = ($page - 1) * $limit;
        $arrParams  = array('limit'       => $limit, 'offset'      => $offset);
        $total        = $tblProduct->countAllItems();
        $arrAllListID = range(1, $total);

        $paginator = Zend_Paginator::factory($arrAllListID);
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($limit);
        $this->view->pagination = $paginator->getPages();
        $this->view->Items = $tblProduct->allItems($arrParams, 'limit');
    }

}